package com.frxuexi.lib_base.net

import com.blankj.utilcode.util.SPUtils
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit
import javax.net.ssl.HostnameVerifier
import javax.net.ssl.SSLSession

object OkhttpUtils {
    //获取OkHttpClient
    val parkClient by lazy {
        OkHttpClient.Builder()
            .connectTimeout(30, TimeUnit.SECONDS)
            .readTimeout(30,TimeUnit.SECONDS)
            .writeTimeout(30,TimeUnit.SECONDS)
            .callTimeout(30,TimeUnit.SECONDS)
            .retryOnConnectionFailure(true)
            .sslSocketFactory(SSLUtils.getSslSocketFactory()!!, SSLUtils.getTrustManager()!!)
            .hostnameVerifier(object :HostnameVerifier{
                override fun verify(hostname: String?, session: SSLSession?): Boolean {
                    //验证host
                    if("10.161.9.80".contains(hostname!!)){
                        return true;
                    }
                    return false;
                }
            })
        .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            .addInterceptor {chain->
                val request = chain.request().newBuilder()
                    .addHeader("accessId","6b7551f94068458d81e888e02e8b53d1")
                    .addHeader("token", SPUtils.getInstance().getString("token","aaa"))
//                    .addHeader("token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhdXRoMCIsImV4cCI6MTc0MjM2MzY5NiwidXNlcm5hbWUiOiJsciJ9.rH3oV2BvvUtWT9mqrX5VN_GrZsI9Q6G80CxGziLCxvQ")
                    .build()
                return@addInterceptor chain.proceed(request)
            }.build()
    }

    val parkRetrofit by lazy {
        Retrofit.Builder()
            .baseUrl(Const.BASE_URL)
            .client(parkClient)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }
}